<template>
  <basic-container :btnFlag=true>
    <div class="card-border">
      <card-header :header-data="headerData"></card-header>
      <!--<div style="float: right;margin-right: 17px;margin-top: 52px;">-->
        <!--<el-button type="primary" size="mini" @click="reportNewVersion()" v-if="isMainDept&&stateControlBtn!='0'">成果上传-->
        <!--</el-button>-->
      <!--</div>-->
      <div class="card-body">
        <avue-crud :data="subjectListData"
                   :option="tableoption"
                   :page="page"
                   :table-loading="tableLoading"
                   @on-load="getResultList"
                   @size-change="sizeChange"
                   @current-change="currentChange"
                   class="avuecrud">
          <template slot="menuRight">
            <el-button type="primary" size="small" v-if="isMainDept&&stateControlBtn!='0'" @click="reportNewVersion()">成果上传</el-button>
          </template>
          <template slot-scope="scope" slot="menu">
            <div class="menu-title" @click.stop="downloadHistoryFile(scope.row,scope.index)">
              <i class="iconfont iconview icon-primary"></i>
              <p class="menu-title-text">下载</p>
            </div>
            <div class="menu-title" @click.stop="subjectHistoryDelete(scope.row,scope.index)" v-if="isMainDept&&subjectState=='5'">
              <i class="iconfont iconalter icon-warning"></i>
              <p class="menu-title-text">删除</p>
            </div>
          </template>
        </avue-crud>
      </div>
      <el-dialog title="成果上传" :visible.sync="dialogVisible" width="1000px">
        <el-form
          label-width="130px"
          :model="projectForm"
          :rules="versionrules"
          ref="projectForm"
        >
          <el-row>
            <el-col :span="11" style="margin-top: 27px;font-weight: 600">
              <el-form-item class="form-item" label="课题名称 :" prop="achievement">
                <span>{{projectForm.subjectTitle}}</span>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row style="font-weight: 600">
            <el-col :span="18">
              <el-form-item class="form-item" label="修改说明 :" prop="updateReason">
                <el-input
                  type="textarea"
                  :autosize="{ minRows: 4, maxRows: 4}"
                  placeholder="请输入修改说明"
                  v-model="projectForm.updateReason"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="11">
              <span style="color: red;float: left;margin: 13px -45px 0px 38px">*</span>
              <el-form-item class="form-item" label="课题报告 :">
                <el-upload
                  action="string"
                  :limit="1"
                  :on-exceed="tipFileOne"
                  :http-request="basisSubmitUploadMixin"
                  :on-change="basisFileChangeMixin"
                  :on-preview="basishandlePreviewMixin"
                  :on-remove="basishandleRemoveMixin"
                  :auto-upload="true"
                  :file-list="basisFileListMixin">
                  <el-button size="small" type="primary">课题报告上传</el-button>
                </el-upload>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="11">
              <el-form-item class="form-item" label="相关材料 :">
                <el-upload
                  ref="uploadrelated"
                  action="string"
                  :http-request="submitUploadMixin"
                  :on-preview="handlePreviewMixin"
                  :on-remove="handleRemoveMixin"
                  :file-list="fileListMixin"
                  :auto-upload="true"
                  :on-change="fileChangeMixin">
                  <el-button slot="trigger" size="small" type="primary">点击上传</el-button>
                </el-upload>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="comitYes()">确定</el-button>
          <el-button @click="cancel()">取 消</el-button>
        </div>
      </el-dialog>
      <xieorg-tree :orgCodeVisiblesubmit="levyDeptCodeVisiblesubmit" @orgCodeClick="levyDeptCodeClick"
                   @orgCodeHandleClosesubmit="levyDeptCodeHandleClosesubmit"
                   @orgCodeCheckChange="levyDeptCodeCheckChange"></xieorg-tree>
    </div>
  </basic-container>
</template>

<script>
  import {
    getSubjectList,
    subjectHistoryDelete,
    addSubjectReport,
    getSubjectReportHistoryList,
    getDeptIds
  } from "@/api/subject";
  import {Subject_DIC} from '@/const/dict';
  import {reportHistoryrules, versionrules} from '@/const/infoRules';
  import basisfile from "@/mixins/basisfile";
  import {filedownload} from "@/api/system";
  import relatedfile from "@/mixins/relatedfile";


  export default {
    name: "subjectReportHistory",
    mixins: [basisfile, relatedfile],
    created() {
      this.projectForm.subjectId = this.$route.params.id;
      this.isMainDept = this.$route.query.isMainDept;
      this.subjectState = this.$route.query.state;
      if (this.$route.query.stateControlBtn == undefined) {
        this.stateControlBtn = '1';
      } else {
        this.stateControlBtn = this.$route.query.stateControlBtn
      }
    },
    data() {
      return {
        headerData: {
          title: '成果管理'
        },
        dialogVisible: false,
        stateControlBtn: '',
        projectForm: {
          fileId: '',
          subjectId: '',
          annexFileIds: [],
          updateReason: '',
          basisFileId: '',
          deptIds: []
        },
        subjectListData: [],
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 10 // 每页显示多少条
        },
        tableoption: {
          page: true,
          menuWidth: 150,
          addBtn: false,
          editBtn: false,
          addRowBtn: false,
          indexLabel: "序号",
          index: true,
          indexWidth:70,
          cellBtn: false,
          delBtn: false,
          columnBtn: false,
          refreshBtn: false,
          filterBtn: false,
          searchBtn: false,
          keyId: "id",
          column: [
            {
              label: "课题名称",
              prop: "subjectTitle",
              cell: true
            },
            {
              label: "修改说明",
              width: 310,
              prop: "updateReason",
              cell: true
            },
            {
              label: "上传人",
              width: 180,
              prop: "uploadNickname",
              cell: true,
//              search: true,
            },
            {
              label: "上传时间",
              width: 110,
              prop: "uploadTime",
              formatter: function (row, value, label, column) {
                if (row.uploadTime != null) {
                  var data = row.uploadTime.split(' ');
                  return data[0];
                }
              },
              cell: true
            }
          ]
        },
        tableLoading: false,
        reportHistoryrules: reportHistoryrules,
        detailDialogVisible: false,
        formtemp: {
          updateReason: '',
          fileId: '',
          fileName: '',
          fileFullName: '',
        },
        versionDetailDeptIds: [],
        levyDeptCodeVisiblesubmit: false,//协同部门树弹窗标志
        versionrules: versionrules,
        basisFileListMixin: [],
        openTypeDic: [
          {
            label: '公开',
            value: '0',
          },
          {
            label: '非公开',
            value: '2',
          },
          {
            label: '部分公开',
            value: '1',
          },
        ],
        isMainDept: false,
      }
    },
    methods: {
      // 课题征集意见跳转this.$route.params.id =subjectDeptId
      subjectReportOpnList(row, index) {
        let src = `/subjectReportOpnList/detail/${this.$route.params.id}`;
        this.$router.push({
          path: src,
          query: {
            version: row.version,
          }
        })
      },
      subjectHistoryDelete(row) {
        this.$confirm("确定删除此版本吗?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          subjectHistoryDelete(row.deptFileId).then(response => {
            if (response.data.code == 0) {
              this.$notify({
                title: '成功',
                message: '删除成功',
                type: 'success',
                duration: 2000
              });
              this.$router.go(0);
            }
          })
        })
      },
      currentChange(currentPage) {
        var page = this.page;
        page.currentPage = currentPage;
        this.getResultList(page);
      },
      sizeChange(pageSize) {
        var page = this.page;
        page.pageSize = pageSize;
        this.getResultList(page);
      },

      getResultList(page) {
        getSubjectReportHistoryList(Object.assign({
          current: page.currentPage,
          size: page.pageSize,
          subjectDeptId: this.$route.params.id
        })).then(response => {
          this.subjectListData = response.data.data.records;
          this.page.total = response.data.data.total;
        })
      },
      reportNewVersion() {
        this.dialogVisible = true;
        this.projectForm.subjectDeptId = this.$route.params.id;
        this.projectForm.subjectTitle = this.$route.query.subjectTitle;
      },
      cancel() {
        this.dialogVisible = false;
      },
      comitYes() {
        if (this.projectForm.basisFileId == '') {
          this.$notify({
            title: "提醒",
            type: "warning",
            message: "请提交课题研究附件",
            duration: 2000
          });
          return;
        }
        if (this.projectForm.needRelease) {
          if (this.projectForm.openType == '') {
            this.$notify({
              title: "提醒",
              type: "warning",
              message: "请选择发布类型",
              duration: 2000
            });
            return;
          } else if (this.projectForm.openType == '1' && this.projectForm.deptIds.length == 0) {
            this.$notify({
              title: "提醒",
              type: "warning",
              message: "请选择公开部门",
              duration: 2000
            });
            return;
          }

        }
        if (this.projectForm.openType == '') {
          this.projectForm.openType = null
        }
        this.$refs.projectForm.validate(valid => {
          if (valid) {
            this.$confirm("确定上传吗?", "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning"
            }).then(() => {
              this.projectForm.fileId = this.projectForm.basisFileId;

              //将多个相关材料的附件id拼成逗号隔开的字符串放在 this.projectForm.fileId 中
              var arr = [];
              for (var item in this.fileListMixin) {
                arr.push(this.fileListMixin[item].url);
              }
              this.projectForm.annexFileIds = arr;

              addSubjectReport(this.projectForm).then(response => {
                if (response.data.code == 0) {
                  this.$notify({
                    title: "成功",
                    message: "发布成功",
                    type: "success",
                    duration: 2000
                  });
                  this.$router.go(0);
                }
              })
            })
          }
        })
      },
      versionDetail(row) {
        this.detailDialogVisible = true;

        this.formtemp.fileId = row.fileId;
        this.formtemp.updateReason = row.updateReason;
        this.formtemp.fileFullName = row.fileFullName;
        var annexForm = {
          subjectDeptId: this.$route.params.id,
          version: row.version,
        };
        getDeptIds(annexForm).then(response => {
          this.versionDetailDeptIds = response.data.data.records;
        })
      },
      detailCancel() {
        this.detailDialogVisible = false
      },
      // 附件下载
      downloadFiletest() {
        filedownload(this.formtemp.fileId).then(response => {
          if (response.status == 200) {
            this.download(response.data, this.formtemp.fileFullName)
          }
        }).catch((error) => {

        })
      },

      // 弹出征集部门弹框
      clickLevyDept() {
        this.levyDeptCodeVisiblesubmit = true
      },
      levyDeptCodeClick() {
      },
      levyDeptCodeHandleClosesubmit() {
        this.levyDeptCodeVisiblesubmit = false
      },
      levyDeptCodeCheckChange(data) {
        this.projectForm.supportDeptName = '';
        this.projectForm.deptIds = [];
        for (var i in data) {
          this.projectForm.supportDeptName += data[i].name + ',';
          this.projectForm.deptIds.push(data[i].id);
        }
      },
      downloadHistoryFile(row) {
        this.$confirm("确定下载此成果吗?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.downloadFile(row.fileId, row.fileFullName)
        })
      },
      // 附件下载
      downloadFile(fileId, fileName) {
        filedownload(fileId).then(response => {
          if (response.status == 200) {
            this.download(response.data, fileName)
          }
        }).catch((error) => {
          this.$notify({
            title: "失败",
            message: "上传失败[" + error + "]",
            type: "error",
            duration: 2000
          });
        })
      },
      // 下载文件
      download(data, filename) {
        if (data == null) {

        } else {
          let url = window.URL.createObjectURL(new Blob([data]));
          let link = document.createElement('a');
          link.style.display = 'none';
          link.href = url;
          link.setAttribute('download', filename);
          document.body.appendChild(link);
          link.click()
        }
      },
    }
  }
</script>

<style lang="scss" scoped>
  .card-body {
    padding: 10px;
    min-height: 400px;
  }

  .detail-data-img {
    width: 23px;
    height: 23px;
    position: relative;
    top: 6px;
  }

  .detail-data-file-tip {
    display: inline-block;
    margin-left: 80px;
  }

  .detail-data-file {
    display: inline-block;
    margin-left: 12px;
  }

  .form-item {
    font-weight: 600;
  }
</style>
<style lang="scss">

  .downbtn {
    padding: 0 0 6px 0px;
    border: 0px solid #dcdfe6;
  }

  .detail-data-img {
    width: 23px;
    height: 23px;
    position: relative;
    top: 6px;
  }

</style>
